﻿using System;
using System.Configuration;
using System.Windows.Forms;

namespace EncryptConnectionStrings
{
    /*TODO: This should be noted that it does not change your physical config found on the disk either. 
    But rather just updates it in memory alone.*/

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            textBoxDpapi.Text = ConfigurationManager.ConnectionStrings["Dpapi"].ConnectionString;
            textBoxRsa.Text = ConfigurationManager.ConnectionStrings["Rsa"].ConnectionString;
        }

        private void buttonSaveDpapi_Click(object sender, EventArgs e)
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            var connectionStringsSection = (ConnectionStringsSection) config.GetSection("connectionStrings");

            connectionStringsSection.ConnectionStrings["Dpapi"].ConnectionString = textBoxDpapi.Text;

            config.Save();
            ConfigurationManager.RefreshSection("connectionStrings");

            textBoxDpapi.Text = ConfigurationManager.ConnectionStrings["Dpapi"].ConnectionString;
        }

        private void buttonSaveRsa_Click(object sender, EventArgs e)
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            var connectionStringsSection = (ConnectionStringsSection) config.GetSection("connectionStrings");

            connectionStringsSection.ConnectionStrings["Rsa"].ConnectionString = textBoxRsa.Text;

            config.Save();
            ConfigurationManager.RefreshSection("connectionStrings");

            textBoxRsa.Text = ConfigurationManager.ConnectionStrings["Rsa"].ConnectionString;
        }
    }
}